package com.liangdemo.web.thread;

/**
 * @Name LiangErLe
 * @date 2022/6/13 10:04
 */
public class ThreadDemo4_6 {
    public static void main(String[] args) {
        MyTask t1 = new MyTask("线程1", 3, true);
        MyTask t2 = new MyTask("线程2", 4, true);
        MyTask t3 = new MyTask("线程3", 1, false);

//使用异步的方式，调用线程
     //   CompletableFuture f1 = CompletableFuture.supplyAsync(() -> t1.call()).thenAccept((result) -> callback(result));
    }

    private static class MyTask extends Thread {

        private Result result = Result.NOTEND;
        private String name;
        private int timeInSeconds;
        private boolean success;

        public MyTask(String name, int timeInSeconds, boolean success) {
            this.name = name;
            this.timeInSeconds = timeInSeconds;
            this.success = success;
        }

        public Result getResult() {
            return result;
        }

        public void run() {
            try {
                Thread.sleep(timeInSeconds * 1000);
                System.out.println(name + "  任务结束");
                result = success ? Result.SUCCESSED : Result.FATLED;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }


    /**
     * 线程执行结果的集中状态
     * NOTEND；正在运行
     * SUCCESSED：成功
     * FATLED：失败
     */
    private static enum Result {
        NOTEND, SUCCESSED, FATLED
    }


    private static void callback(Boolean result) {
        if (false == result) {

        }
    }

}
